﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using IntoCommon.Util;
using IntoStationSystem.Db;
using IntoStationSystem.Global;
using IntoStationSystem.ViewModels;
using Microsoft.SqlServer.Server;
using Microsoft.VisualBasic.Logging;

namespace IntoStationSystem.ExDevice
{
    /// <summary>
    /// 打印设备
    /// </summary>
    public class PrintDev
    {

        //型号
        public string ProModel = string.Empty;

        private BarTender.Format btFormat;
        private BarTender.Application btApp = new BarTender.Application();
        public void PrintLabels()
        {
            try
            {
                
                var template = ProModel;
                string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{template}Normal.btw");
                DbHelper.Log($"开始打印，模板：{file}");
                if (File.Exists(file))
                {
                    try
                    {

                        //if (Workstation.PrintDate < DateTime.Today)//时间到第二天，更新序列号
                        //{
                        //    Workstation.PrintDate = DateTime.Today;
                        //    Workstation.PrintNum = 0;
                        //}

                        bool IsPrintName = false;
                        foreach (var printer in PrinterSettings.InstalledPrinters)
                        {
                            if (printer.ToString().Contains(Fields.PrintName))
                            {
                                IsPrintName=true;
                                break;
                            }
                        }
                        if(!IsPrintName)
                        {

                            DbHelper.Log("打印机名称不存在");
                            return;
                        }



                        btFormat = btApp.Formats.Open(file);
                        btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
                        //btFormat.SetNamedSubStringValue("Year", year);
                        //btFormat.SetNamedSubStringValue("Month", month);
                        //btFormat.SetNamedSubStringValue("Number", "1");
                        //btFormat.SetNamedSubStringValue("Index", Workstation.PrintNum.ToString("D4"));
                        btFormat.Printer = Fields.PrintName;
                        btFormat.PrintOut(false, false);
                        btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
                        
                        
                        DbHelper.Log("打印完成");
                        //打印标签，完成后回复2
                        Locator.Instance.Service.ResponseCollection(4);
                    }
                    catch (Exception e)
                    {
                        DbHelper.Log("打印报错：" + e.Message);
                        //Log.Error(e, "打印");
                    }
                }
                else
                {
                    DbHelper.Log("模板不在："+ ProModel);
                    //MessageBox.Show("标签模板文件不存在!");
                }
            }
            catch (Exception e)
            {
                //Log.Error(e, "打印");
            }
        }
    }
}
